//
//  BaseViewController.m
//  SheMeasured
//
//  Created by ddkj on 2019/4/4.
//  Copyright © 2019 xmddkj. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _navBarStyle = NavigationBarStyle_Normal;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    
    [self setupNavigationBarStyle];
    //统一处理present出来的VC的dismiss
    if (!self.tabBarController && self.navigationController.viewControllers.count == 1) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    }
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
}

- (void)setupNavigationBarStyle {
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    if (self.navBarStyle == NavigationBarStyle_Normal)
    {
        [self setupNormalNavigation];
    }
    else if (self.navBarStyle == NavigationBarStyle_Clear)
    {
        [self setupClearNavigation];
    }
    else if (self.navBarStyle == NavigationBarStyle_Hidden)
    {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}

- (void)setupNormalNavigation {
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    
//    UIImage *image = [UIImage imageNamed:@"nav_bgimg"];
//    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    
//    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName:[UIColor blackColor]}];
}

- (void)setupClearNavigation {
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationBar.translucent = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor clearColor]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
